const { join } = require('path');
const { src, dest } = require('gulp');
const nunjucksRender = require('gulp-nunjucks-render');
const { paths } = require('./utils');

const data = {
  cssPath: './css',
  jsPath: './js',
  imagePath: './images',
  fontPath: './fonts',
};
const filters = {
  shorten: function (str, count) {
    return str.slice(0, count || 5);
  },
};

function manageEnvironment(env) {
  Object.keys(filters).forEach((filterName) => {
    env.addFilter(filterName, filters[filterName]);
  });
}

function template() {
  return src(join(paths.dev.template, '*.njk'))
    .pipe(
      nunjucksRender({
        path: [paths.dev.template],
        data,
        ext: '.html',
        envOptions: {
          autoescape: true,
          trimBlocks: true,
          lstripBlocks: true,
        },
        manageEnv: manageEnvironment,
      })
    )
    .pipe(dest(paths.prod.template));
}

exports.template = template;
